﻿using System.Collections.Generic;
using System.Linq;
using System.Configuration;

namespace Actya.Web.Infrastructure.ElFinderConnector.Configuration
{
	public static class Configuration
	{
		public static string RootPath { get; private set; }

		public static string RootUrl { get; private set; }

		public static string DotFiles { get; private set; }

		public static string UplMaxSize { get; private set; }

		public static string TmbDir { get; private set; }

		public static string TmbUrl { get; private set; }

		public static int TmbsAtOnce { get; private set; }

		public static string ArcAppPath { get; private set; }

		public static int CacheDuration { get; private set; }

		public static IEnumerable<string> DisabledCommands { get; private set; }

		public static IEnumerable<string> DisabledMimeTypes { get; private set; }

		public static IEnumerable<string> ArchivesMimeTypes { get; private set; }

		public static IDictionary<string, string[]> AllowedMimeTypes { get; private set; }

		public static bool IsMediumTrust { get; private set; }

		public static void Init(System.Web.HttpContext context)
		{
			var section = (ElFinderSection)ConfigurationManager.GetSection("ElFinder");

			TmbsAtOnce = section.Thumbnails.TmbAtOnce;

			CacheDuration = section.CacheDuration;

			TmbDir = context.Server.MapPath(section.Thumbnails.TmbDirectory);

			TmbUrl = section.Thumbnails.TmbUrl;

			RootPath = context.Server.MapPath(section.Root.Path);

			RootUrl = section.Root.Url;

			DotFiles = section.DotFiles;

			UplMaxSize = section.UplMaxSize + "M";

			ArcAppPath = section.ArcApp.ArcAppPath;

			if (section.DisabledCommands.Count > 0)
			{
				DisabledCommands = section.DisabledCommands.Cast<NamedElement>().Where(x => x.Name != string.Empty).Select(x => x.Name);
			}
			else
			{
				DisabledCommands = new List<string>();
			}

			if (section.DisabledMimeTypes.Count > 0)
			{
				DisabledMimeTypes = section.DisabledMimeTypes.Cast<NamedElement>().Where(x => x.Name != string.Empty).Select(x => x.Name);
			}
			else
			{
				DisabledMimeTypes = new List<string>();
			}

			if (section.ArchivesMimeTypes.Count > 0)
			{
				ArchivesMimeTypes = section.ArchivesMimeTypes.Cast<NamedElement>().Where(x => x.Name != string.Empty).Select(x => x.Name);
			}
			else
			{
				ArchivesMimeTypes = new List<string>();
			}
		}

		public static void Init(ConnectorConfiguration actyaConfig)
		{
			TmbsAtOnce = actyaConfig.TmbsAtOnce;
			CacheDuration = actyaConfig.CacheDuration;
			TmbDir = actyaConfig.TmbDir;
			TmbUrl = actyaConfig.TmbUrl;
			RootPath = actyaConfig.RootPath;
			RootUrl = actyaConfig.RootUrl;
			DotFiles = actyaConfig.DotFiles;
			UplMaxSize = actyaConfig.UplMaxSizeMb + "M";
			ArcAppPath = actyaConfig.ArcAppPath;
			DisabledCommands = actyaConfig.DisabledCommands;
			DisabledMimeTypes = actyaConfig.DisabledMimeTypes;
			ArchivesMimeTypes = actyaConfig.ArchivesMimeTypes;
			AllowedMimeTypes = actyaConfig.AllowedMimeTypes;
			IsMediumTrust = actyaConfig.IsMediumTrust;
		}
	}
}
